//
//  GYDToolBar.swift
//  Weibo
//
//  Created by 贵永冬 on 16/8/1.
//  Copyright © 2016年 贵永冬. All rights reserved.
//

import UIKit

class GYDToolBar: UIView {
    var toolBarBlock : ((index:Int) -> ())?
    var lastBtn : UIButton?
    var selected : Bool = false
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        createSubViews()

    }
    private func createSubViews() {
        let imageUrl = ["compose_toolbar_picture","compose_mentionbutton_background","compose_trendbutton_background","compose_emoticonbutton_background","compose_toolbar_more"]
        let hiImageUrl = ["compose_toolbar_picture_highlighted","compose_mentionbutton_background_highlighted","compose_trendbutton_background_highlighted","compose_emoticonbutton_background_highlighted","compose_toolbar_more_highlighted"]
        let width : CGFloat = self.width / CGFloat(imageUrl.count)
        let heigth = self.height
        for i in 0 ..< imageUrl.count {
            let image = imageUrl[i]
            let hiImage = hiImageUrl[i]
            let button = UIButton(type: UIButtonType.Custom)
            button.setImage(UIImage(named: image), forState: UIControlState.Normal)
            button.setImage(UIImage(named: hiImage), forState: UIControlState.Highlighted)
            button.frame = CGRect(x: CGFloat(i)*width, y: 0, width: width, height: heigth)
            self.addSubview(button)
            button.tag = 80+i
            button.addTarget(self, action: #selector(GYDToolBar.buttonClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        }
        let lineView = UIView(frame: CGRect(x: 0, y: 0, width: self.width, height: 0.5))
        lineView.backgroundColor = RGB(210, g: 210, b: 210)
        self.addSubview(lineView)
    }
    @objc private func buttonClick(btn : UIButton) {
        
        if btn.tag == 83 {
            if lastBtn == btn {
                if selected {
                    btn.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: UIControlState.Normal)
                    btn.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: UIControlState.Highlighted)
                }else {
                    btn.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: UIControlState.Normal)
                    btn.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: UIControlState.Highlighted)
                }
                selected = !selected
            }else {
                btn.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: UIControlState.Normal)
                btn.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: UIControlState.Highlighted)
            }
        }else {
            selected = false
        }
        
        lastBtn = btn
        if self.toolBarBlock != nil {
            self.toolBarBlock!(index : btn.tag - 80)
        }
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

}
